<?php

class Finishes extends Controller {

	function Finishes()
	{
		parent::Controller();	
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('productsmodel');       
	}     
    
    function index()
    {

        $data["list"] = $this->productsmodel->admin_get_all_finishes();
        $data["response"] = get_response();        

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'products'));  
        $this->load->view("admin/default/nav_products", array('selected' => 'finishes'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/finishes/finishes_list', $data);   
        $this->load->view('admin/default/footer');   

    }
 
    function edit($id = NULL)
    {

        $this->load->library("form_validation");
                                
        if($id == NULL) show_404();

        $data["item"] = $this->productsmodel->admin_get_finish($id); 
        if(empty($data["item"])){
            set_response('error', "The finish type you're looking for does not exist");
            redirect('admin/finishes');
        }

        $data["products"] = $this->productsmodel->admin_get_finish_products($id);
         
        if($this->input->post("submit_action") !== FALSE){

            $new_name = ucfirst(trim($this->input->post("name", TRUE)));
        
            if((strtoupper($new_name) == strtoupper($data["item"]["Name_Finishes"])) || ($this->_validate_form())){

                $data = array('Name_Finishes' => $new_name);
                $this->productsmodel->admin_update_finish($id, $data);
                set_response('success', "The product finish type <i>".$new_name."</i> was updated.");
                redirect('admin/finishes');

            }

        }

        $data["response"] = get_response(); 

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'products'));  
        $this->load->view("admin/default/nav_products", array('selected' => 'finishes'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/finishes/finishes_item', $data);   
        $this->load->view('admin/default/footer');   

    }

    function remove($id = false){

        if($id == NULL) show_404();
        $data["item"] = $this->productsmodel->admin_get_finish($id); 

        if(empty($data["item"])){
            set_response('error', "The finish type you're attempting to delete does not exist");
            redirect('admin/finishes');
        } else {
            $data["products"] = $this->productsmodel->admin_get_finish_products($id);
            $r = "You're about to permanently delete this Finish type";
            $r .= '<div class="buttons_block">';
            $r .= '<a class="button" href="'.site_url('admin/finishes/confirm_removal/'.$data["item"]["id_Finishes"]).'">CONFIRM DELETION</a> ';
            $r .= '<a class="button" href="'.site_url('admin/finishes').'">CANCEL</a>';
            $r .= '</div>';
            set_response('warning', $r);
            redirect('admin/finishes/edit/'.$id);
        }

    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->productsmodel->admin_get_finish($id);

        if(empty($data["item"])){
            set_response('error', "The finish type you're attempting to delete does not exist");
            redirect('admin/finishes');
        } 

        $this->productsmodel->admin_delete_finish($id);
        $this->productsmodel->admin_delete_finish_products($id);
        set_response('success', "The Finish type <i>".$data["item"]["Name_Finishes"]."</i> was deleted.");
        redirect('admin/finishes');
    }
                           
    //  returns in json format: response:string, success:boolean, [item:array]
    public function create()
    {   

        $this->load->library("form_validation");                     
        $name = ucfirst(strip_tags($this->input->post('name', TRUE)));

        if($this->_validate_form()){

            $data = array('Name_Finishes' => $name);
            $new_id = $this->productsmodel->admin_create_new_finish($name);

            $data = array(
                "id" => $new_id,
                "name" => quotes_to_entities($name)
            );
    
            $r = array(
                'response' => '<span class="success">A new finish type was created</span>',
                'success' => TRUE,
                'item' => $data);

        } else {

            $r = array(
                'response' => validation_errors(),
                'success' => FALSE);

        }

        echo json_encode($r);   
    }   

    function deactivate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Finishes' => 0);
        $this->productsmodel->admin_update_finish($id, $data);
        $item = $this->productsmodel->admin_get_finish($id);
        set_response('success', "The product finish type <i>".$item["Name_Finishes"]."</i> is now inactive.");
        redirect('admin/finishes');
    }
    function activate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Finishes' => 1);
        $this->productsmodel->admin_update_finish($id, $data);  
        $item = $this->productsmodel->admin_get_finish($id);
        set_response('success', "The product finish type <i>".$item["Name_Finishes"]."</i> is now active.");
        redirect('admin/finishes');
    }

    private function _validate_form(){
        
        $config = array(
                        array(
                         'field'   => 'name',
                         'label'   => 'Finish name',
                         'rules'   => 'trim|xss_clean|required|max_length[80]|callback_check_duplicated'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

     public function check_duplicated($name){ 

        if($this->productsmodel->is_duplicated('finishes','Name_Finishes',$name)){
            $this->form_validation->set_message('check_duplicated', "A finish type with that name already exists");
            return FALSE;
        }else{
            return TRUE;    
        }
    } 

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */